/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary.display;

import boundary.report.Bill;
import boundary.report.letterLeftOver;
import control.Application;
import entity.Billing;
import entity.Entity;
import entity.Sample;
import entity.ServiceRequest;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jasperreports.engine.JRException;

/**
 *
 * @author Juan
 */
public class PServiceRequestDisplay extends EntityDisplayPanel {

    /**
     * Creates new form PServiceRequestRegistration
     */
    public PServiceRequestDisplay() {
        initComponents();
    }

    @Override
    public void setCurrentEntity(Entity entity) throws IllegalArgumentException {
        if (entity instanceof ServiceRequest) {
            currentEntity = entity;
            ServiceRequest currentServiceRequest = (ServiceRequest) entity;
            LServiceRequestNumber.setText(currentServiceRequest.toString());
            LClient.setText(currentServiceRequest.getClient().toString());
            TFOrigin.setText(currentServiceRequest.getOrigin());
            TFShipmentRecord.setText(currentServiceRequest.getShipmentRecord());
            Billing billing = currentServiceRequest.getBilling();
            if (billing.billingPending()) {
                Double deposit = billing.getDeposit();
                BBill.setEnabled(true);
                if (deposit != null && deposit > 0.0) {
                    TFBilling.setText("Abono: $" + deposit);
                } else {
                    TFBilling.setText("Facturación pendiente");
                }
            } else if (billing.getServiceOrder() != null) {
                TFBilling.setText("Orden de servicio: " + billing.getServiceOrder());
            } else {
                TFBilling.setText(billing.getBillNumber().toString());
            }
            String dispositionMethod;
            Integer dispositionMethod1 = currentServiceRequest.getDispositionMethod();
            if (dispositionMethod1.equals(Sample.DISPOSITION_METHOD_DELIVER)) {
                dispositionMethod = "Entregar";
            } else {
                dispositionMethod = "Disponer";
            }
            TFDispositionMethod.setText(dispositionMethod);
            pSamplesInput.setSamples(currentServiceRequest.getSamples());
        } else {
            throw new IllegalArgumentException("The object to be displayed is not a Service Request.");
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        LServiceRequestNumber = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        TFOrigin = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        TFShipmentRecord = new javax.swing.JTextField();
        TFBilling = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        pSamplesInput = new components.panels.PSamplesInput();
        BCancel = new javax.swing.JButton();
        LClient = new javax.swing.JLabel();
        TFDispositionMethod = new javax.swing.JTextField();
        BBill = new javax.swing.JButton();
        BLeftoverLetter = new javax.swing.JButton();
        BPrintBarcodes = new javax.swing.JButton();

        jLabel1.setText("Número de Solicitud:");

        LServiceRequestNumber.setText("SA");

        jLabel2.setText("Cliente:");

        jLabel3.setText("Lugar de Origen:");

        TFOrigin.setEditable(false);

        jLabel4.setText("Registro de Envío:");

        jLabel5.setText("Control de Cobros:");

        TFShipmentRecord.setEditable(false);

        TFBilling.setEditable(false);

        jLabel6.setText("Disposición de Muestras:");

        BCancel.setText("Cerrar");
        BCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCancelActionPerformed(evt);
            }
        });

        LClient.setForeground(new java.awt.Color(0, 0, 255));
        LClient.setText("jLabel7");
        LClient.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        LClient.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LClientMouseClicked(evt);
            }
        });

        TFDispositionMethod.setEditable(false);

        BBill.setText("Facturar Solicitud");
        BBill.setEnabled(false);
        BBill.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BBillActionPerformed(evt);
            }
        });

        BLeftoverLetter.setText("Imprimir Carta de Devolución de Sobrantes");
        BLeftoverLetter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BLeftoverLetterActionPerformed(evt);
            }
        });

        BPrintBarcodes.setText("Imprimir Códigos");
        BPrintBarcodes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BPrintBarcodesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pSamplesInput, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(TFOrigin)
                    .addComponent(TFShipmentRecord)
                    .addComponent(TFBilling)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(LServiceRequestNumber))
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(LClient)
                            .addComponent(TFDispositionMethod, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(BCancel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(BPrintBarcodes)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(BBill)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(BLeftoverLetter)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(LServiceRequestNumber))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(LClient)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFOrigin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFShipmentRecord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFBilling, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFDispositionMethod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pSamplesInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BCancel)
                    .addComponent(BBill)
                    .addComponent(BLeftoverLetter)
                    .addComponent(BPrintBarcodes))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void BCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCancelActionPerformed
        buttonTabComponent.closeTab();
    }//GEN-LAST:event_BCancelActionPerformed

    private void LClientMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LClientMouseClicked
        ServiceRequest currentServiceRequest = (ServiceRequest) currentEntity;
        Application.displayEntity(currentServiceRequest.getClient());
    }//GEN-LAST:event_LClientMouseClicked

    private void BBillActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BBillActionPerformed
        Bill newBill = new Bill((ServiceRequest) currentEntity);
        try {
            newBill.Print();
            Desktop.getDesktop().open(new java.io.File("FacturaPDF.pdf"));
        } catch (JRException ex) {
            Logger.getLogger(PServiceRequestDisplay.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PServiceRequestDisplay.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_BBillActionPerformed

    private void BLeftoverLetterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BLeftoverLetterActionPerformed
        letterLeftOver llo = new letterLeftOver((ServiceRequest) currentEntity);
        try {
            llo.Print();
            Desktop.getDesktop().open(new File("report2PDF.pdf"));
        } catch (JRException ex) {
            Logger.getLogger(PServiceRequestDisplay.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PServiceRequestDisplay.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_BLeftoverLetterActionPerformed

    private void BPrintBarcodesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BPrintBarcodesActionPerformed
        entity.ServiceRequest sr = (entity.ServiceRequest) currentEntity;
        control.ControlSample.printBarcodes(sr.getSamples());
    }//GEN-LAST:event_BPrintBarcodesActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BBill;
    private javax.swing.JButton BCancel;
    private javax.swing.JButton BLeftoverLetter;
    private javax.swing.JButton BPrintBarcodes;
    private javax.swing.JLabel LClient;
    private javax.swing.JLabel LServiceRequestNumber;
    private javax.swing.JTextField TFBilling;
    private javax.swing.JTextField TFDispositionMethod;
    private javax.swing.JTextField TFOrigin;
    private javax.swing.JTextField TFShipmentRecord;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private components.panels.PSamplesInput pSamplesInput;
    // End of variables declaration//GEN-END:variables
}
